﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using CombatAutomationTheater;
using XMLCharSheets.Annotations;

namespace XMLCharSheets
{
    [DataContract]
    public class LightControlViewModel : INotifyPropertyChanged
    {


        private int _radius;
        private int _stepChange;
        private string _radiusLabel;
        private PathfinderLightType _selectedLight = new PathfinderLightType(PathfinderLightType.LightType.Disabled);
        private bool _useSet;
        private bool _useStep;

        public LightControlViewModel(String label)
        {
            RadiusLabel = label;
        }

        [DataMember]
        public bool UseSet
        {
            get { return _useSet; }
            set
            {
                if (value.Equals(_useSet)) return;
                _useSet = value;
                OnPropertyChanged("UseSet");
            }
        }

        [DataMember]
        public bool UseStep
        {
            get { return _useStep; }
            set
            {
                if (value.Equals(_useStep)) return;
                _useStep = value;
                OnPropertyChanged("UseStep");
            }
        }

        [DataMember]
        public String RadiusLabel
        {
            get { return _radiusLabel; }
            set
            {
                if (value == _radiusLabel) return;
                _radiusLabel = value;
                OnPropertyChanged("RadiusLabel");
            }
        }

        [DataMember]
        public int Radius
        {
            get { return _radius; }
            set
            {
                if (value == _radius) return;
                _radius = value;
                OnPropertyChanged("Radius");
            }
        }

        [DataMember]
        public int StepChange
        {
            get { return _stepChange; }
            set
            {
                if (value == _stepChange) return;
                _stepChange = value;
                OnPropertyChanged("StepChange");
            }
        }

        [DataMember]
        public PathfinderLightType SelectedLight
        {
            get { return _selectedLight; }
            set
            {
                if (Equals(value, _selectedLight)) return;
                _selectedLight = value;
                OnPropertyChanged("SelectedLight");
                OnPropertyChanged("Description");
            }
        }

        public string Description

        {
            get
            {
                if (SelectedLight == null)
                {
                    return "NA";
                }
                if (UseSet)
                {
                    return SelectedLight.CurrentLightType + " " + Radius;
                }
                else
                {
                    string linedesc = "-";
                    if (StepChange >= 0)
                    {
                        linedesc = "+";
                    }
                    return linedesc+""+StepChange + " " + Radius;
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        public PathfinderLightType CombineLights(PathfinderLightType environmental)
        {
            if (UseSet)
            {
                return SelectedLight;
            }
            else
            {
                return environmental.CreateLightByStep(StepChange);
            }
        }

        public static void SetToPrimary(LightControlViewModel vm)
        {
            vm.Radius = 20;
            vm.UseSet = true;
            vm.SelectedLight =
                PathfinderLightList.PossibleLights.First(x => x.CurrentLightType == PathfinderLightType.LightType.Normal);
        }
        public static void SetToSecondary(LightControlViewModel vm)
        {
            vm.Radius = 40;
            vm.UseStep = true;
            vm.StepChange = 1;
            vm.SelectedLight =
    PathfinderLightList.PossibleLights.First(x => x.CurrentLightType == PathfinderLightType.LightType.Normal);
            
        }
    }
}
